package manager.qrcode;

import org.apache.log4j.Logger;

import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.demo.trade.config.Configs;
import com.alipay.demo.trade.model.builder.AlipayTradePrecreateRequestBuilder;
import com.alipay.demo.trade.model.result.AlipayF2FPrecreateResult;
import com.alipay.demo.trade.service.AlipayTradeService;
import com.alipay.demo.trade.service.impl.AlipayTradeServiceImpl;

import advice.ExceptionManager;
import database.pojo.UserRechargeInfo;

public class AlipayQrcode {
	// 支付宝当面付2.0服务
    private static AlipayTradeService   tradeService;
    private static Logger logger = Logger.getLogger(ExceptionManager.class);

    
    static {
        Configs.init("zfbinfo.properties");

        tradeService = new AlipayTradeServiceImpl.ClientBuilder().build();

    }
	public String getAlipayQrcode(UserRechargeInfo uInfo,String notifyUrl) {
		String qrcode ="";
        String outTradeNo =uInfo.getRecharge_orderid();
        String subject ="乐音坊包时套餐";
        String totalAmount = Double.toString(uInfo.getMount());
        totalAmount="0.01";//测试
        // 卖家支付宝账号ID，用于支持一个签约账号下支持打款到不同的收款账号，(打款到sellerId对应的支付宝账号)
        // 如果该字段为空，则默认为与支付宝签约的商户的PID，也就是appid对应的PID
        //String sellerId = "";

        String body = uInfo.getRecharge_description();
        // (必填) 商户门店编号，通过门店号和商家后台可以配置精准到门店的折扣信息，详询支付宝技术支持
        String storeId = Integer.toString(uInfo.getMid());
        // 支付超时，定义为120分钟
        String timeoutExpress = "10m";
        // 创建扫码支付请求builder，设置请求参数
        logger.info("OUTtradeNo:"+outTradeNo+" totalAmount:"+totalAmount+" mid:"+storeId);
        AlipayTradePrecreateRequestBuilder builder = new AlipayTradePrecreateRequestBuilder()
            .setSubject(subject).setTotalAmount(totalAmount).setOutTradeNo(outTradeNo)
            .setBody(body)
            .setStoreId(storeId)
            .setTimeoutExpress(timeoutExpress)                           
        	.setNotifyUrl(notifyUrl);//支付宝服务器主动通知商户服务器里指定的页面http路径,根据需要设置
    
        AlipayF2FPrecreateResult result = tradeService.tradePrecreate(builder);
        switch (result.getTradeStatus()) {
            case SUCCESS:
                AlipayTradePrecreateResponse response = result.getResponse();
                qrcode = response.getQrCode();
                //System.out.println("response.getQrCode:"+response.getQrCode());
                logger.info("response.getQrCode:"+response.getQrCode());
                break;

            case FAILED:
               
                break;

            case UNKNOWN:
               
                break;

            default:
              
                break;
        }
		return qrcode;
	}

}
